var Adv_skd=require("./Adv_skd")
var e = require;
var t = module;
var o = exports;
var n,
    i = e("EncryptUtil").EncryptType,
    a = o,
    r =
        ((n = function (e, t) {
            return (n =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var o in t) t.hasOwnProperty(o) && (e[o] = t[o]);
                })(e, t);
        }),
        function (e, t) {
            function o() {
                this.constructor = e;
            }
            n(e, t), (e.prototype = null === t ? Object.create(t) : ((o.prototype = t.prototype), new o()));
        }),
    s = function (e, t, o, n) {
        var i,
            a = arguments.length,
            r = a < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, o)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(e, t, o, n);
        else
            for (var s = e.length - 1; s >= 0; s--)
                (i = e[s]) && (r = (a < 3 ? i(r) : a > 3 ? i(t, o, r) : i(t, o)) || r);
        return a > 3 && r && Object.defineProperty(t, o, r), r;
    },
    c = function (e, t, o, n) {
        return new (o || (o = Promise))(function (i, a) {
            function r(e) {
                try {
                    c(n.next(e));
                } catch (t) {
                    a(t);
                }
            }
            function s(e) {
                try {
                    c(n.throw(e));
                } catch (t) {
                    a(t);
                }
            }
            function c(e) {
                var t;
                e.done
                    ? i(e.value)
                    : ((t = e.value),
                      t instanceof o
                          ? t
                          : new o(function (e) {
                                e(t);
                            })).then(r, s);
            }
            c((n = n.apply(e, t || [])).next());
        });
    },
    l = function (e, t) {
        var o,
            n,
            i,
            a,
            r = {
                label: 0,
                sent: function () {
                    if (1 & i[0]) throw i[1];
                    return i[1];
                },
                trys: [],
                ops: []
            };
        return (
            (a = {next: s(0), throw: s(1), return: s(2)}),
            "function" == typeof Symbol &&
                (a[Symbol.iterator] = function () {
                    return this;
                }),
            a
        );
        function s(e) {
            return function (t) {
                return c([e, t]);
            };
        }
        function c(a) {
            if (o) throw new TypeError("Generator is already executing.");
            for (; r; )
                try {
                    if (
                        ((o = 1),
                        n &&
                            (i = 2 & a[0] ? n.return : a[0] ? n.throw || ((i = n.return) && i.call(n), 0) : n.next) &&
                            !(i = i.call(n, a[1])).done)
                    )
                        return i;
                    switch (((n = 0), i && (a = [2 & a[0], i.value]), a[0])) {
                        case 0:
                        case 1:
                            i = a;
                            break;
                        case 4:
                            return r.label++, {value: a[1], done: !1};
                        case 5:
                            r.label++, (n = a[1]), (a = [0]);
                            continue;
                        case 7:
                            (a = r.ops.pop()), r.trys.pop();
                            continue;
                        default:
                            if (!(i = (i = r.trys).length > 0 && i[i.length - 1]) && (6 === a[0] || 2 === a[0])) {
                                r = 0;
                                continue;
                            }
                            if (3 === a[0] && (!i || (a[1] > i[0] && a[1] < i[3]))) {
                                r.label = a[1];
                                break;
                            }
                            if (6 === a[0] && r.label < i[1]) {
                                (r.label = i[1]), (i = a);
                                break;
                            }
                            if (i && r.label < i[2]) {
                                (r.label = i[2]), r.ops.push(a);
                                break;
                            }
                            i[2] && r.ops.pop(), r.trys.pop();
                            continue;
                    }
                    a = t.call(e, r);
                } catch (s) {
                    (a = [6, s]), (n = 0);
                } finally {
                    o = i = 0;
                }
            if (5 & a[0]) throw a[1];
            return {value: a[0] ? a[1] : void 0, done: !0};
        }
    };
Object.defineProperty(a, "__esModule", {value: !0});
var d = e("AppFacade"),
    u = e("LoadBundlesCommand"),
    p = e("LoadResCommand"),
    h = cc._decorator,
    f = h.ccclass,
    _ = h.property,
    y = (function (e) {
        function t() {
            var t = (null !== e && e.apply(this, arguments)) || this;
            return (
                (t.node_content = null),
                (t.lab_tips = null),
                (t.progressBar = null),
                (t.bundleNames = []),
                (t.assetsModules = []),
                (t.param = {}),
                t
            );
        }
        return (
            r(t, e),
            (t.prototype.onLoad = function () {
                (this.node_content.active = !1),
                    (this.node.width = cc.winSize.width),
                    (this.node.height = cc.winSize.height),
                    (this.node.x = 0),
                    (this.node.y = 0);
            }),
            (t.prototype.init = function () {
                this.param.isShow
                    ? ((this.node.opacity = 255), (this.node.getComponent(cc.BlockInputEvents).enabled = !0))
                    : ((this.node.opacity = 0), (this.node.getComponent(cc.BlockInputEvents).enabled = !1));
            }),
            (t.prototype.start = function () {
                Adv_skd.default.instance.getWxIdFromServer(()=>{

                    var e = this;
                    this.addEvent(),
                        (this.lab_tips.string = "正在加载资源，请稍后"),
                        (this.progressBar.progress = 0),
                        (this.needLoadBundles(this.bundleNames) || this.needLoadRes(this.assetsModules)) &&
                            (this.node_content.active = !0),
                        this.loadBundles(this.bundleNames)
                            .then(function () {
                                return e.loadRes(e.assetsModules);
                            })
                            .then(function () {
                                e.node.destroy(), e.successCallback && e.successCallback();
                            }),
                        cc.SDK.init(
                            {
                                game_code: "wsas",
                                banner_code: "efed5881246841abb1dbd4d0ddb6180b",
                                video_ad_code: "976b1a16d89743d8b0618d517f11b6fa",
                                custom_code: "0417971bd1984674a324c381645dfb77",
                                boxPortalCode: "5d4217afe6d24c68aaf89c3caa1c1273"
                            },
                            i.ED_LQ
                        );
                    for (
                        var t = ["game", "popup", "public", "start"],
                            o = function (e) {
                                var o = t[e];
                                cc.assetManager.loadBundle(o, function (e) {
                                    e
                                        ? console.error("Failed to load subpackage:", e)
                                        : console.log("Subpackage loaded successfully!", o);
                                });
                            },
                            n = 0;
                        n < t.length;
                        n++
                    )
                        o(n);
                },()=>{
                    
                var e = this;
                this.addEvent(),
                    (this.lab_tips.string = "正在加载资源，请稍后"),
                    (this.progressBar.progress = 0),
                    (this.needLoadBundles(this.bundleNames) || this.needLoadRes(this.assetsModules)) &&
                        (this.node_content.active = !0),
                    this.loadBundles(this.bundleNames)
                        .then(function () {
                            return e.loadRes(e.assetsModules);
                        })
                        .then(function () {
                            e.node.destroy(), e.successCallback && e.successCallback();
                        }),
                    cc.SDK.init(
                        {
                            game_code: "wsas",
                            banner_code: "efed5881246841abb1dbd4d0ddb6180b",
                            video_ad_code: "976b1a16d89743d8b0618d517f11b6fa",
                            custom_code: "0417971bd1984674a324c381645dfb77",
                            boxPortalCode: "5d4217afe6d24c68aaf89c3caa1c1273"
                        },
                        i.ED_LQ
                    );
                for (
                    var t = ["game", "popup", "public", "start"],
                        o = function (e) {
                            var o = t[e];
                            cc.assetManager.loadBundle(o, function (e) {
                                e
                                    ? console.error("Failed to load subpackage:", e)
                                    : console.log("Subpackage loaded successfully!", o);
                            });
                        },
                        n = 0;
                    n < t.length;
                    n++
                )
                    o(n);
                })

            }),
            (t.prototype.addEvent = function () {
                game.appFacade.view.registerObserver(
                    u.default.LOAD_BUNDLES_PROGRESS,
                    new puremvc.Observer(this.onLoadBundleProgress, this)
                ),
                    game.appFacade.view.registerObserver(
                        p.default.LOAD_RES_PROGRESS,
                        new puremvc.Observer(this.onLoadResProgress, this)
                    );
            }),
            (t.prototype.removeEvent = function () {
                game.appFacade.view.removeObserver(u.default.LOAD_BUNDLES_PROGRESS, this),
                    game.appFacade.view.removeObserver(p.default.LOAD_RES_PROGRESS, this);
            }),
            (t.prototype.onDestroy = function () {
                this.removeEvent();
            }),
            (t.prototype.needLoadBundles = function (e) {
                for (var t = 0; t < e.length; t++) if (!game.BundleManager.instance.getBundle(e[t])) return !0;
                return !1;
            }),
            (t.prototype.needLoadRes = function (e) {
                return game.AssetsManager.instance.getNeedLoadAssetsBundleNames(e).length > 0;
            }),
            (t.prototype.onLoadBundleProgress = function (e) {
                var t = e.getBody().progress,
                    o = e.getBody().totalBundleCount,
                    n = e.getBody().currBundleIndex;
                (this.progressBar.progress = t),
                    (this.lab_tips.string = "正在下载游戏资源，请稍后(" + n + "/" + o + ")");
            }),
            (t.prototype.onLoadResProgress = function (e) {
                var t = e.getBody().progress;
                this.progressBar.progress = t;
            }),
            (t.prototype.loadBundles = function (e) {
                var t = this;
                return new Promise(function (o) {
                    return c(t, void 0, void 0, function () {
                        var t = this;
                        return l(this, function () {
                            return (
                                this.needLoadBundles(e)
                                    ? ((this.lab_tips.string = "正在下载游戏资源，请稍后"),
                                      game.appFacade.sendNotification(d.default.LOAD_BUNDLES, {
                                          bundleNames: e,
                                          finishCallBack: function () {
                                              o(!0);
                                          },
                                          failCallBack: function () {
                                              game.UIManager.instance.showDialog(
                                                  "资源下载失败，是否重试?",
                                                  !0,
                                                  function () {
                                                      t.loadBundles(e).then(function () {
                                                          o(!0);
                                                      });
                                                  },
                                                  function () {
                                                      cc.game.end();
                                                  }
                                              );
                                          }
                                      }))
                                    : o(!0),
                                [2]
                            );
                        });
                    });
                });
            }),
            (t.prototype.loadRes = function (e) {
                var t = this;
                return new Promise(function (o) {
                    return c(t, void 0, void 0, function () {
                        var t = this;
                        return l(this, function () {
                            return (
                                this.needLoadRes(e)
                                    ? ((this.lab_tips.string = "正在加载游戏资源，请稍后"),
                                      game.appFacade.sendNotification(d.default.LOAD_RES, {
                                          assetsModules: e,
                                          finishCallBack: function () {
                                              o(!0);
                                          },
                                          failCallBack: function () {
                                              game.UIManager.instance.showDialog(
                                                  "资源加载失败，是否重试?",
                                                  !0,
                                                  function () {
                                                      t.loadRes(e).then(function () {
                                                          o(!0);
                                                      });
                                                  },
                                                  function () {
                                                      cc.game.end();
                                                  }
                                              );
                                          }
                                      }))
                                    : o(!0),
                                [2]
                            );
                        });
                    });
                });
            }),
            s([_(cc.Node)], t.prototype, "node_content", void 0),
            s([_(cc.Label)], t.prototype, "lab_tips", void 0),
            s([_(cc.ProgressBar)], t.prototype, "progressBar", void 0),
            s([f], t)
        );
    })(cc.Component);
a.default = y;
